package gui.data.D3;
import static util.Util.checkNumeric;
import gui.ErrorMessage;
import gui.MyFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import util.NumericCheckWrapper;

import calcs.TriangleCalculator;
import calcs.XYZDistanceCalculator;

public class PointsIn3DInputGui extends JPanel{
	JTextField x1i, x2i, y1i, y2i, z1i, z2i;
	MyFrame myFrame;
	JLabel x1, x2, y1, y2, z1, z2;
	
	public PointsIn3DInputGui(final MyFrame myFrame) {
		this.myFrame = myFrame;
		this.setLayout(new GridLayout(7, 2));
		this.setPreferredSize(new Dimension(500, 500));
		x1 = new JLabel("X1 = ");
		x2 = new JLabel("X2 = ");
		y1 = new JLabel("Y1 = ");
		y2 = new JLabel("Y2 = ");
		z1 = new JLabel("Z1 = ");
		z2 = new JLabel("Z2 = ");
		x1i = new JTextField();
		x2i = new JTextField();
		y1i = new JTextField();
		y2i = new JTextField();
		z1i = new JTextField();
		z2i = new JTextField();
		JButton calculate = new JButton("Calculate");
		calculate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				openResult();
			}
		});
		this.add(x1);
		this.add(x1i);
		this.add(y1);
		this.add(y1i);
		this.add(z1);
		this.add(z1i);
		this.add(x2);
		this.add(x2i);
		this.add(y2);
		this.add(y2i);
		this.add(z2);
		this.add(z2i);
		this.add(new JLabel(""));
		this.add(calculate);
	}

	private void openResult() {
		boolean error = false;
		JTextField[] textFields = {x1i, y1i, z1i, x2i, y2i, z2i};
		Double[] values = new Double[6];
		for(int i = 0; i<textFields.length; i++){
			NumericCheckWrapper res = checkNumeric(textFields[i].getText());
			if(!res.isCorrect()){
				error = true;
				new ErrorMessage("Check input!");
				break;
			}else{
				values[i] = res.getValue();
			}
		}
		
		if(error){
			error = false;
		}else{
			new XYZDistanceCalculator(values[0], values[1], values[2], values[3], values[4], values[5], myFrame);
		}
	}
}
